package org.com.suit.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.com.suit.common.DateUtil;
import org.com.suit.dao.WritingDao;
import org.com.suit.domain.Writing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class WrtingController {
	
	@Autowired
	WritingDao dao;
	
	
	/* メッセージーを書き込む */
	@RequestMapping(value="/sendMessage.do", method=RequestMethod.POST)
	public String writingText(@ModelAttribute Writing writing, @RequestParam String no, HttpServletResponse response) {
		
		String today = DateUtil.getCurrentTime();
	
		writing.setNo(Integer.valueOf(no));
		writing.setRegdate(today);
		
		dao.writingText(writing);
					
		return "redirect:readWritingLists.do";
	}
	
	/* 書き込みリスト呼び出し */
	@SuppressWarnings("unchecked")
	@RequestMapping("/readWritingLists.do")
	public String getWritingLists(HttpSession session, Model model) {
		
		
		Map<String, String> map = new HashMap<String, String>();
		map = (Map<String, String>) session.getAttribute("myInfo");
		int no = Integer.valueOf(map.get("no"));
		
		List<Writing> list;
		list = dao.getWritingLists(no);
		if(list !=null){
			model.addAttribute("writingLists", list);
		}else{
			list = new ArrayList<Writing>();
		}
			
		return "mainHome";
	}
	
	
}
